package com.edu.bean;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

/*
 * @Author:mx
 * @Date:2025/11/7-14:59:53
 */
public class MyIOC2 {

    //以 map集合的形式 存容器 创建的对象。key是beanId，值是对应的对象
    private Map<String, Object> box;
    //用以 存Properties配置文件中的信息
    private Properties pro;
    //用于装 配置文件里的 属性的信息
    private Properties attrs;

    public MyIOC2(String path) {
        this.box = new HashMap<>();
        this.pro = new Properties();
        this.attrs = new Properties();
        readProperties(path);//读取到配置文件，信息存放在pro理
        init();//解析配置文件 并创建对象
    }

    public void init() {
        Set<String> beanIds = pro.stringPropertyNames();
        for (String key : beanIds) {
            String value = pro.getProperty(key);
            try {
                if(key.contains(".")){
                   attrs.setProperty(key,value);
                }else {
                    //创建对象
                    Class clazz = Class.forName(value);
                    Object o = clazz.newInstance();
                    box.put(key, o);
                }
            } catch (ClassNotFoundException e) {
                System.out.println("类型不存在");
                e.printStackTrace();
            } catch (InstantiationException e) {
                System.out.println("没有无参数构造方法");
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                System.out.println("无参数构造方法不可被访问");
                e.printStackTrace();
            }
        }
        setAttr();
    }
    // 从attrs 读取 对象.属性=值
    public void setAttr(){
        //key - 对象.属性
        for (String key : attrs.stringPropertyNames()) {
            String value = attrs.getProperty(key);
            int index = key.indexOf(".");
            String beanId = key.substring(0,index);
            String attrName = key.substring(index+1);
            Object o = box.get(beanId);
            if(o == null){
                continue;
            }
            Class clazz = o.getClass();
            try {
                Field field = clazz.getDeclaredField(attrName);
                field.setAccessible(true);
                Class fileType = field.getType();
                field.set(o,transType(value,fileType));
            } catch (NoSuchFieldException e) {
                System.out.println("在对象"+o+"中不存在"+attrName+"这个属性");
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }

    public void readProperties(String path) {
        InputStream inputStream = MyIOC2.class.getClassLoader().getResourceAsStream(path);
        try {
            pro.load(inputStream);
        } catch (IOException e) {
            System.out.println("配置文件不存在");
            e.printStackTrace();
        }
    }

    public Object transType(String str,Class targetClass){
        if(targetClass == int.class){
            return Integer.parseInt(str);
        }
        if(targetClass == float.class){
            return Float.parseFloat(str);
        }
        return str;
    }


    public Object getBean(Class clazz) {
        for (String s : box.keySet()) {
            Object o = box.get(s);
            if (o.getClass() == clazz) {
                return o;
            }
        }
        return null;
    }

    public Object getBean(String beanId) {
        return box.get(beanId);
    }
}
